package com.zyb.system.employee.service.impl;


import com.zyb.base.service.impl.BaseServiceImpl;
import com.zyb.constans.BaseConstants;
import com.zyb.system.employee.domain.Employee;
import com.zyb.system.employee.domain.EmployeeBo;
import com.zyb.system.employee.mapper.EmployeeMapper;
import com.zyb.system.employee.service.IEmployeeService;
import com.zyb.system.login.domain.PhoneRegister;
import com.zyb.system.logininfo.domain.Logininfo;
import com.zyb.system.logininfo.service.impl.LogininfoServiceImpl;
import com.zyb.system.user.domain.User;
import com.zyb.utils.MD5Utils;
import com.zyb.utils.StrUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogininfoServiceImpl logininfoService;

    @Override
    public List<Employee> queryAllByDo() {
        List<EmployeeBo> lists = employeeMapper.loadAllBo();
        ArrayList<Employee> employeeList = new ArrayList<>();
        for (EmployeeBo employeeBo : lists) {
            Employee employee = new Employee();
            BeanUtils.copyProperties(employeeBo,employee);
            employeeList.add(employee);

        }
        return employeeList;
    }

    @Override
    public void add(PhoneRegister register) {
        Employee employee = new Employee();
        //补充数据
        //生成盐值
        String salt = StrUtils.getComplexRandomString(32);
        employee.setSalt(salt);
        //盐值加密
        String saltPassword = MD5Utils.encrypByMd5(salt + register.getPassword());
        employee.setPassword(saltPassword);
        employee.setUsername(register.getPhone());
        employee.setPhone(register.getPhone());
        employeeMapper.save(employee);
        Logininfo logininfo = new Logininfo();
        //复制数据
        BeanUtils.copyProperties(employee,logininfo);
        //设置状态
        logininfo.setType(BaseConstants.Logininfo.STATE_MANAGER);
        logininfo.setDisable(BaseConstants.Logininfo.DISABLE_0);
        //保存数据
        logininfoService.add(logininfo);
        employee.setLogininfo_id(logininfo.getId());
        //保存数据
        employeeMapper.update(employee);
    }

    @Override
    public Employee queryByPhone(String phone) {
        return employeeMapper.loadByPhone(phone);
    }
}
